/**
 * 
 *
 * https://www.top.iridescent
 *
 * 版权所有，侵权必究！
 */

package top.iridescent.generate.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import top.iridescent.db.model.DataSourceCreate;
import top.iridescent.db.service.DynamicDataSourceService;
import top.iridescent.generate.service.SysGeneratorService;
import top.iridescent.generate.service.SysGeneratorService;
import top.iridescent.generate.utils.PageUtils2;
import top.iridescent.generate.utils.Query;
import top.iridescent.model.Result;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 代码生成器
 * 
 * @author 
 */
@Controller
@RequestMapping("/sys/generator")
@CrossOrigin
@Api(tags = "生成代码")
public class SysGeneratorController {
	@Autowired
	private SysGeneratorService sysGeneratorService;
	@Autowired
	private DynamicDataSourceService dataSourceService;

	@ResponseBody
	@GetMapping("/ds/list")
	@ApiOperation(value = "数据源下拉框")
	public Result dsList(){
		return dataSourceService.dsList();
	}

	@ResponseBody
	@GetMapping("/change")
	@ApiOperation(value = "切换数据源")
	public Result changeCurrentDataSource(@RequestParam(value = "dataSourceName") String dataSourceName){
		return dataSourceService.changeCurrentDataSource(dataSourceName);
	}

	@ResponseBody
	@PostMapping("/add")
	@ApiOperation(value = "添加数据源")
	public Result addDataSource(@RequestBody DataSourceCreate create){
		return dataSourceService.addDataSource(create);
	}


	/**
	 * 列表
	 */
	@ResponseBody
	@GetMapping("/list")
	@ApiOperation(value = "表列表检索")
	public Result list(@RequestParam Map<String, Object> params){
		String dataSourceName=(String) params.get("dataSourceName");
			PageUtils2 pageUtil = sysGeneratorService.queryList(new Query(params));
			return Result.ok().setData(pageUtil);

	}
	
	/**
	 * 生成代码
	 */
	@ApiOperation(value = "生成代码 ")
	@GetMapping("/code")
	public void code(String tables,String dataSourceName, HttpServletResponse response) throws IOException{
		byte[] data = sysGeneratorService.generatorCode(tables.split(","),dataSourceName);

//		response.reset(); // 非常重要
//		response.setCharacterEncoding("UTF-8");
//
//		response.setHeader("conent-type", "application/octet-stream");
//		response.setContentType("application/octet-stream");
//		response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("renren.zip", "UTF-8"));
//		response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
//		response.setHeader("Access-Control-Allow-Origin", "*");
		//OutputStream out = response.getOutputStream();
		//out.write(data);


		response.reset();
		response.setHeader("Content-Disposition", "attachment; filename=\"generate.zip\"");
		response.addHeader("Content-Length", "" + data.length);
		response.setContentType("application/octet-stream; charset=UTF-8");



		IOUtils.write(data, response.getOutputStream());
	}
}
